package com.HeadlineSystem.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.HeadlineSystem.pojo.Userinfo;
import com.HeadlineSystem.service.UserInfoService;
import com.HeadlineSystem.service.impl.UserInfoServiceImpl;
import com.HeadlineSystem.util.Constr;

public class UserInfoServlet extends HttpServlet {

	private static final long serialVersionUID = -774822931668233830L;

	private UserInfoService uis=new UserInfoServiceImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if(action==null||"login".equals(action)){
			findUserLogin(request, response);
		}
	}

	private void findUserLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out= response.getWriter();
		
		String account =request.getParameter("account");
		String password =request.getParameter("password");
		Userinfo ui=uis.findUserLogin(account,password);
		
		if(ui==null){
			out.write("<script type=\"text/javascript\">alert('账号密码错误');location.href='jsp/login.jsp'</script>");
		}else
			request.getSession().setAttribute(Constr.user_info, ui);
		response.sendRedirect("jsp/profile_home.jsp");
	}

}
